package cn.smile.config.spring.web;

import cn.smile.config.spring.interceptor.AccessRestriction;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: MaoSuyu
 * @User：John
 * @Date: 2019/5/17
 * @Time: 14:32
 * @Description: No Description
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 配置web消息转换器
     *
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //配置字符集,该配置可在全局配置文件中配置，这里可注释掉
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        //配置序列化
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //配置日期的显示格式
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //添加配置到容器中
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastJsonHttpMessageConverter);
    }

    /**
     * 配置拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AccessRestriction()).addPathPatterns("/**");
    }

    /**
     * 配置静态资源的处理，在全局配置文件中已经配置，这里不再配置
     * @param registry
     */
    /*@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }*/
}
